package file.day9_20.day05.构建服务端与客户端练习02;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    private Socket socket;

    public Client(){
        System.out.println("正在连接服务器");

        try {
            socket = new Socket("localhost",8090);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("服务器连接成功");
    }

    public void start(){
        try(
                PrintWriter pw = new PrintWriter(
                        new BufferedWriter(
                                new OutputStreamWriter(
                                        socket.getOutputStream(),"UTF-8"
                                )
                        ),true
                );
                ) {

            Scanner scanner = new Scanner(System.in);
            while (true){
                System.out.println("请输入要发送的信息：");
                String message = scanner.nextLine();
                if(message.equals("exit"))break;
                pw.println(message);
                System.out.println("发送完成");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }
}
